package com.xu.cloud.common.config;

import com.xu.cloud.common.advice.XuCloudWrapperResponseBodyAdvice;
import com.xu.cloud.common.feign.FeignRequestHeadersInterceptor;
import com.xu.cloud.common.feign.FeignRequestOption;
import com.xu.cloud.common.feign.ResultTypeFeignDecoder;
import feign.Logger;
import org.springframework.beans.factory.ObjectFactory;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.http.HttpMessageConverters;
import org.springframework.cloud.openfeign.FeignAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * feign配置
 *
 * @author xuguan
 * @since 2025/3/6
 */
@Configuration
@AutoConfigureAfter({FeignAutoConfiguration.class, XuCloudAdviceAutoConfiguration.class})
@ConditionalOnClass(feign.Feign.class)
public class FeignConfig {

    @Bean
    @ConditionalOnMissingBean
    public feign.Request.Options feignRequestOption() {
        return new FeignRequestOption();
    }

    @Bean
    @ConditionalOnMissingBean
    public feign.RequestInterceptor feignRequestInterceptor() {
        return new FeignRequestHeadersInterceptor();
    }

    @Bean
    @ConditionalOnMissingBean
    @ConditionalOnBean(XuCloudWrapperResponseBodyAdvice.class)
    public feign.codec.Decoder feignDecoder(ObjectFactory<HttpMessageConverters> messageConverters) {
        return new ResultTypeFeignDecoder(messageConverters);
    }

    @Bean
    @ConditionalOnMissingBean
    public Logger.Level feignLoggerLevel() {
        return Logger.Level.BASIC;
    }
}
